//模块基类
Ext.namespace('app');

app.module = function(tab){
    this.main = tab;
    this.getFileViewDlg=function(projectid){
        mainPanel = this;
        Ext.Ajax.request({
            method:'post',
            url:'project/selectById.do',
            params:{
                id: projectid
            },
            scope: this,
            success: function(resp, opts) {
                var result = Ext.util.JSON.decode(resp.responseText);
                var htmlStr = '<div class="mytable"><table width="100%" height="95%" align="center" cellspacing="0"><tr><th colspan="4">课题名称：';
                if(result.key.name==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.name;
                }
                htmlStr += '</th></tr><tr><td class="spec">课题类型：</td><td>';
                if(result.key.projectTypeName==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.projectTypeName;
                }
                if(result.key.iscross==false){
                    htmlStr += '(纵向)';
                } else {
                    htmlStr += '(横向)';
                }
                htmlStr += '</td><td>课题来源：</td><td>';
                if(result.key.approvedept==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.approvedept;
                }
                htmlStr += '</td></tr><tr><td class="specalt">课题状态：</td><td>';
                if(result.key.state==0 || result.key.state==1){
                    htmlStr += '申报中';
                } else if(result.key.state==2) {
                    htmlStr += '申报失败';
                } else if(result.key.state==11) {
                    htmlStr += '已结题';
                } else {
                    htmlStr += '在研';
                }
                htmlStr += '</td><td>立项时间：</td><td>';
                if(result.key.approvaltime==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += Ext.util.Format.date(new Date(parseInt(result.key.approvaltime)), 'Y-m');
                }
                htmlStr += '</td></tr><tr><td class="spec">预计结题时间：</td><td class="alt">';
                if(result.key.planendtime==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += Ext.util.Format.date(new Date(parseInt(result.key.planendtime)), 'Y')+'年';
                }
                htmlStr += '</td><td>结题时间：</td><td>';
                if(result.key.endtime==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += Ext.util.Format.date(new Date(parseInt(result.key.endtime)), 'Y-m');
                }
                htmlStr += '</td></tr><tr><td class="specalt">下拨经费：</td><td class="alt">';
                if(result.key.money==null){
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.money;
                }
                htmlStr += '</td><td class="alt">学院配套经费：</td><td class="alt">';
                if(result.key.shoolmoney==null) {
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.shoolmoney;
                }
                htmlStr += '</td></tr><tr><td class="spec">负责人：</td><td class="alt">';
                var names = result.key.names;
                if(names==null || names=='') {
                    htmlStr += '&nbsp;';
                } else if(names.indexOf(",")==-1){
                    htmlStr += names;
                } else {
                    htmlStr += names.substring(0,names.indexOf(","));
                }
                htmlStr += '</td><td>负责人部门：</td><td>';
                if(result.key.deptname==null) {
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.deptname;
                }
                htmlStr += '</td></tr><tr><td class="specalt">参与人员：</td><td colspan="3" class="alt">';
                if(names.indexOf(",")!=-1){
                    htmlStr += names.substr(names.indexOf(",")+1);
                } else {
                    htmlStr += '&nbsp;';
                }
                htmlStr += '</td></tr><tr><td class="spec">备注：</td><td colspan="3" class="alt">';
                if(result.key.description==null) {
                    htmlStr += '&nbsp;';
                } else {
                    htmlStr += result.key.description;
                }
                htmlStr += '</td></tr><tr><td class="specalt">文件：</td><td colspan="3" class="alt">';
                if(result.key.filename!=null){
                    var fileArray = result.key.filename.split(":");
                    for(var i=0;i<fileArray.length;i++){
                        htmlStr += '<p><a href="file/download.do?projectid='+result.key.id+'&filename='
                            + encodeURIComponent(encodeURIComponent(fileArray[i])) + '">' + fileArray[i]+'</a></p>';
                    }
                } else {
                    htmlStr += '&nbsp;';
                }
                htmlStr += '</td></tr></table></div>';
                var dlg = new Ext.Window({
                    height: 400,
                    width: 500,
                    layout: 'fit',
                    border: false,
                    modal: true,
                    resizable: true,
                    autoScroll:true,
                    html:htmlStr
                });
                var title = result.key.remarks;
                if(title==null){
                    title = '无';
                }
                dlg.setTitle('课题编号：'+title);
                dlg.show();
            }
        });
    };
    this.init();
}

Ext.extend(app.module, Ext.util.Observable, {
    init : Ext.emptyFn
});

//主程序类
app.kygl = function(){
    this.init();
}

Ext.extend(app.kygl, Ext.util.Observable, {
    init: function(){
        mainPanel = this;
        var navPanel = new Ext.Panel({
            id: 'navPanel',
            title: '导航菜单',
            iconCls:'mn1',
            width: 190,
            collapsible: true,
            region: 'west',
            split: true,
            layout: 'accordion',
            layoutConfig:{
                activeOnTop : false,             //设置打开的子面板置顶
                fill : true,                     //子面板充满父面板的剩余空间
                hideCollapseTool: false,         //显示“展开收缩”按钮
                titleCollapse : true,     //允许通过点击子面板的标题来展开或收缩面板
                animate:true,          //使用动画效果
                autoWidth:true
            }
        });
        Ext.Ajax.request({
            method:'post',
            url:'user/menus.do',
            scope: this,
            success: function(resp, opts) {
                var json = Ext.util.JSON.decode(resp.responseText).key;
                var menus = json[0].children;
                for(var i=0;i<menus.length;i++){
                    var tree = new Ext.tree.TreePanel({
                        id: 'menu'+menus[i].id,
                        width: 180,
                        collapsible: true,
                        title: "<img align='top' class='IEPNG' src='" +menus[i].icon + "'/>" + menus[i].text,
                        autoScroll: true,
                        root: {
                            id: menus[i].id,
                            text: menus[i].text,
                            expanded: true,
                            children: menus[i].children
                        },
                        lines:false,        // 去掉树的线
                        rootVisible : false// 根节点是否可见
                    });
                    tree.on('click',this.clickTree, this);
                    navPanel.add(tree);
                }
                navPanel.doLayout();
            },
            failure: function(resp,opts) {
                Ext.Msg.alert('提示', '菜单加载失败,请刷新页面重试');
            }
        });

        var viewport = new Ext.Viewport({
            layout: 'border',
            items: [
                {
                    id: 'appMaintabs',
                    xtype: 'tabpanel',
                    title: '科研管理系统',
                    margins:'0 5 0 0',
                    activeTab: 0,
                    autoScroll: true,
                    region: 'center',
                    items: [
                        {
                            xtype: 'panel',
                            title: '首页',
                            contentEl: 'center',
                            bodyStyle: 'padding:10px'
                        }
                    ]
                },
                navPanel,
                {
                    xtype: 'panel',
                    collapsible: true,
                    frame: true,
                    title: '科研管理系统',
                    region: 'north',
                    contentEl: 'north'
                },
                {
                    xtype: 'box',
                    height: 17,
                    region: 'south',
                    contentEl: 'south'
                }
            ]
        });
        this.body = Ext.getCmp('appMaintabs');
        this.loadMask = new Ext.LoadMask(this.body.body);
    },
    //点击菜单方法(node:被点击的节点)
    clickTree: function(node){
        if(!node.leaf){
            return false;
        }
        var id = 'tab-' + node.id;
        var tab = Ext.getCmp(id);
        if(!tab){
            //限制选项卡数量，超过6就把第二个干掉
            if(this.body.items.getCount()>=2){
                this.body.remove(1,true);
            }
            tab = this.body.add(new Ext.Panel({
                id: id,
                title: "<img align='top' class='IEPNG' src='" + node.ui.getIconEl().src + "'/>" + node.text,
                layout: 'fit',
                closable: true,
                layoutOnTabChange: true
            }));
            this.body.setActiveTab(tab);
            //加载模块
            this.loadModel(node.attributes.url,tab);
        }else{
            this.body.setActiveTab(tab);
        }
    },

    //加载模块方法(id:模块ID;tab:模块显示在哪里)
    loadModel: function(id,tab){
        //定义模块变量
        var model;
        if(this[id]){
            //如果模块类已存在，就直接实例化
            model = new this[id](tab);
        }else{
            this.loadMask.show();
            Ext.Ajax.request({
                method:'GET',
                url: 'module/'+id+'.js',
                scope: this,
                success: function(response){
                    //获取模块类
                    this[id] = eval(response.responseText);
                    //实例化模块类
                    model = new this[id](tab);
                    this.loadMask.hide();
                }
            });
        }
    }
});

//实例化主程序类
Ext.BLANK_IMAGE_URL = 'js/ext/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'title';
Ext.onReady(function(){
    //初始化Ext.QuickTips，以使得tip提示可用
    Ext.QuickTips.init();
    myApp = new app.kygl();
    var changePwdButton = Ext.get('changePwdButton');
    changePwdButton.on('click',function(){
        var changePwdForm = new Ext.form.FormPanel({
            labelAlign: 'right',
            bodyStyle: 'padding:5px',
            buttonAlign: 'right',
            frame: true,
            url:'user/changePwd.do',
            labelWidth: 80,
            allowBlank: false,
            msgTarget: 'side',
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'textfield',
                    name: 'oldpassword',
                    allowBlank: false,
                    inputType: 'password',
                    fieldLabel: '旧密码'
                },
                {
                    xtype: 'textfield',
                    name: 'password',
                    allowBlank: false,
                    inputType: 'password',
                    fieldLabel: '新密码'
                },
                {
                    xtype: 'textfield',
                    name: 'repassword',
                    inputType: 'password',
                    allowBlank: false,
                    fieldLabel: '重复输入'
                }
            ],
            buttons: [{
                text: '提交',
                scope: this,
                handler: function() {
                    if (changePwdForm.getForm().isValid()) {
                        var oldpassword = changePwdForm.getForm().findField('oldpassword').getValue();
                        var password = changePwdForm.getForm().findField('password').getValue();
                        var repassword = changePwdForm.getForm().findField('repassword').getValue();
                        if(password==repassword){
                            if(oldpassword==password){
                                Ext.Msg.alert('提示', '新密码好像和旧密码一样');
                            } else {
                                changePwdForm.getForm().submit({
                                    waitTitle: '保存数据',
                                    waitMsg: '正在保存……',
                                    scope: this,
                                    method: 'POST',
                                    submitEmptyText: false,     //禁止提交emptyText
                                    success: function(changePwdForm, action) {
                                        if (action.result.success=='true') {
                                            Ext.Msg.alert('信息', action.result.info);
                                            changePwdDlg.close();
                                        } else {
                                            Ext.Msg.alert('信息', action.result.info);
                                        }
                                    },
                                    failure: function(changePwdForm, action) {
                                        Ext.Msg.alert('提示', action.result.info);
                                    }
                                });
                            }
                        } else {
                            Ext.Msg.alert('提示', '两次输入不一致');
                        }
                    }
                }
            }]
        });
        var changePwdDlg = new Ext.Window({
            height: 170,
            width: 300,
            layout: 'fit',
            border: false,
            frame: true,
            title: '修改密码',
            modal: true,
            plain: true,
            resizable: false,
            items: [changePwdForm],
            listeners: {
                scope: this,
                render: function(fp){
                    changePwdForm.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = changePwdForm.getForm();
                    f.clearInvalid();
                }
            }
        });
        changePwdDlg.show();
    });
});